package com.ruiyou.tb;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.ProgressBar;

import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import android.view.*;

public class UWebChromeClient extends WebChromeClient {
    private View mainView;
    private ProgressBar pb;
    private Activity activity;
    private boolean isX5Core;

    public UWebChromeClient(Activity activity, View mainView, ProgressBar pb, boolean isX5Core) {
        this.activity=activity;
        this.mainView=mainView;
        this.pb=pb;
        this.isX5Core=isX5Core;
    }

    @Override
    public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
        super.onShowCustomView(view, customViewCallback);
        if (!isX5Core){
			setFullScreen(true);
            activity.setContentView(view);
        }
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
        if (!isX5Core){
			setFullScreen(false);
            activity.setContentView(mainView);
        }
    }

    @Override
    public void onProgressChanged(WebView webView, int i) {
        super.onProgressChanged(webView, i);
        if (pb != null) {
            pb.setProgress(i);
        }
    }
    
    private void setFullScreen(boolean bl){
        if (isX5Core) {
            return;
        }
        int orientFlag=bl? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
		int winFlag1=WindowManager.LayoutParams.FLAG_FULLSCREEN;
		int winFlag2=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        activity.setRequestedOrientation(orientFlag);
		Window win=activity.getWindow();
		if(bl){
			win.setFlags(winFlag1,winFlag1);
			win.setFlags(winFlag2,winFlag2);
		}else{
			win.clearFlags(winFlag1);
			win.clearFlags(winFlag2);
		}
    }
}
